package com.honeywen.mymvc.leetcode;

/**
 * 86. 分隔链表
 * https://leetcode-cn.com/problems/partition-list/
 *
 * @author wangwei
 * @date 2019/6/10
 */
public class PartitionTest {


    public static ListNode partition(ListNode head, int x) {

        ListNode node = head;

        ListNode low = new ListNode(0);
        ListNode high = new ListNode(0);
        ListNode l = low;
        ListNode h = high;


        while (node != null) {
            if (node.val < x) {

                // 去左边
                low.next = node;
                low = low.next;

            } else {
                high.next = node;
                high = high.next;

            }
            node = node.next;
        }

        high.next = null;

        low.next = h.next;

        return l.next;
    }


    public static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(4);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(2);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(2);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = n6;

        print(n1);
        print(partition(n1, 3));


    }

    private static void print(ListNode node) {
        System.out.println();
        while (node != null) {
            System.out.print(node.val + "->");
            node = node.next;
        }
        System.out.println();

    }

}
